﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using MS.InformationManagement.Presenter.Support;
using MS.InformationManagement.Presenter.DocumentPrinter;
using System.Text;
using MS.InformationManagement.DAL.DocumentCenter;
using MS.InformationManagement.Controls._CONTROLTEMPLATES.InformationManagerControls;
using MS.InformationManagement.Presenter.ObserverImplementation;
using System.Collections.Generic;
using Microsoft.SharePoint;

namespace MS.InformationManagement.WP.DocumentCenter.DocumentPrinter
{
    public partial class DocumentPrinterUserControl : BaseControl, IDocumentPrinterListView, IBaseSubject
    {
        #region [Private Fields]

        private DocumentPrinterListPresenter _Presenter;

        #endregion

        #region [Public Members]

        public List<IBaseObserver> ObservableCollection { get; set; }

        #endregion

        #region [Event Handlers]

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                //Register observers

                ObservableCollection = new List<IBaseObserver>();
                this.RegisterObserver(Filed);
                this.RegisterObserver(NonFiled);
                this.RegisterObserver(Printed);
                
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
                
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        protected void NonFiled_OnPageChanged(object sender, EventArgs e)
        {
            try
            {
                _Presenter.FetchUserDocuments(Microsoft.SharePoint.SPContext.Current.Web.CurrentUser.LoginName);
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void Filed_OnPageChanged(object sender, EventArgs e)
        {
            try
            {
                _Presenter.FetchRecordDocuments(SPContext.Current.Web.CurrentUser.LoginName);
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        #endregion

        #region [IDocumentPrinterListView Members]

        #endregion

        #region Implemented Subject Methods

        public void RegisterObserver(IBaseObserver observer)
        {
            this.ObservableCollection.Add(observer);
            observer.ParentSubject = this;
        }

        public void NotifyChanges()
        {
            foreach (IBaseObserver observer in ObservableCollection)
                observer.ExecuteAction();
        }

        #endregion



    }
}
